package com.aiyi.education.user.service.impl;


import com.aiyi.education.entity.KsUserDepartment;
import com.aiyi.education.entity.KsUserDepartmentWithBLOBs;
import com.aiyi.education.mapper.KsUserDepartmentMapper;
import com.aiyi.education.user.model.FindsDepartsChildren;
import com.aiyi.education.user.model.SysDepartTreeModel;
import com.aiyi.education.user.service.IKsUserDepartmentService;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户部门 服务实现类
 * </p>
 *
 * @author renyong
 * @since 2020-04-22
 */
@Service
public class KsUserDepartmentServiceImpl extends ServiceImpl<KsUserDepartmentMapper, KsUserDepartmentWithBLOBs> implements IKsUserDepartmentService {


    @Override
    public List<SysDepartTreeModel> queryTreeList() {
        LambdaQueryWrapper<KsUserDepartmentWithBLOBs> query = new LambdaQueryWrapper<KsUserDepartmentWithBLOBs>();
        query.orderByAsc(KsUserDepartmentWithBLOBs::getOrderId);
        List<KsUserDepartmentWithBLOBs> list = this.list(query);
        // 调用wrapTreeDataToTreeList方法生成树状数据
        List<SysDepartTreeModel> listResult = FindsDepartsChildren.wrapTreeDataToTreeList(list);
        return listResult;
    }

    /**
     * 根据部门id删除并且删除其可能存在的子级任何部门
     */
    @Override
    public boolean delete(Integer deptId) {
        List<Integer> idList = new ArrayList<>();
        idList.add(deptId);
        this.checkChildrenExists(deptId, idList);
        boolean ok = this.removeByIds(idList);
        return ok;
    }

    @Override
    public Boolean updateDepartDataById(KsUserDepartmentWithBLOBs sysDepart, String username) {
        if (sysDepart != null && username != null) {
            this.updateById(sysDepart);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean saveDepartData(KsUserDepartmentWithBLOBs sysDepart, String username) {
        if (sysDepart != null && username != null) {
            if (sysDepart.getParentId() == null) {
                sysDepart.setParentId(0);
            }
            // 先判断该对象有无父级ID,有则意味着不是最高级,否则意味着是最高级
            // 获取父级ID
            sysDepart.setParentId(sysDepart.getParentId());
            sysDepart.setAddDate(new Date());
            return this.save(sysDepart);
        }
        return false;
    }

    /**
     * delete 方法调用
     * @param id
     * @param idList
     */
    private void checkChildrenExists(Integer id, List<Integer> idList) {
        LambdaQueryWrapper<KsUserDepartmentWithBLOBs> query = new LambdaQueryWrapper<KsUserDepartmentWithBLOBs>();
        query.eq(KsUserDepartment::getParentId,id);
        List<KsUserDepartmentWithBLOBs> departList = this.list(query);//所有子部门
        if(departList != null && departList.size() > 0) {
            for(KsUserDepartmentWithBLOBs depart : departList) {
                idList.add(depart.getDepartId());
                this.checkChildrenExists(depart.getDepartId(), idList);
            }
        }
    }
}
